home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / netlog-1.02 / extract / grammar.y < prev    next >
Encoding:
Lex Description  |  1993-04-20  |  8.5 KB  |  431 lines

  1. %{
  2. /*
  3.      extract - A network log processor
  4.      Copyright (C) 1993 Douglas Lee Schales, David K. Hess, David R. Safford
  5.  
  6.      Please see the file `COPYING' for the complete copyright notice.
  7.  
  8. grammar.y - 03/20/93
  9.  
  10. */
  11. #include <stdio.h>
  12. #include <memory.h>
  13. #include <netdb.h>
  14. #include <sys/types.h>
  15. #include <netinet/in.h>
  16. #include <malloc.h>
  17. #include "lex.h"
  18. #include "chario.h"
  19. #include "parser.h"
  20. #include "builder.h"
  21. #include "timesub.h"
  22. #include "stdunix.h"
  23.  
  24. extern int parse_error;
  25. #define TCPDATA 0
  26. #define UDPDATA 1
  27. extern int udptcpflag;
  28.  
  29. %}
  30.  
  31. %union {
  32.      unsigned int intval;
  33.      char *strval;
  34. }
  35.  
  36. %token DSTPORT SRCPORT DSTADDR SRCADDR SRCNET DSTNET DATE TIME HOST NET
  37. %token TODAY YEST
  38. %token OR AND MASKOPER NOT SINCE BEFORE
  39. %token NEQ LEQ GEQ
  40. %token INTEGER BYTEVAL STRING
  41. %token PRINT NEXT
  42. %type <intval> INTEGER BYTEVAL
  43. %type <intval> OR AND
  44. %type <intval> oper NEQ LEQ GEQ '=' '<' '>' '!' NOT
  45. %type <intval> netop
  46. %type <intval> DATE TIME TODAY YEST SINCE BEFORE
  47. %type <intval> SRCADDR DSTADDR SRCPORT DSTPORT SRCNET DSTNET HOST NET
  48. %type <intval> PRINT NEXT
  49. %type <strval> STRING
  50. %type <intval> ip_address netmask hostspec integer tcpport datespec timespec
  51. %left OR
  52. %left AND
  53. %right '!'
  54. %%
  55. file
  56.      : stmt file
  57.      | stmt 
  58. ;
  59.  
  60. stmt
  61.      : cond '{' actionset '}' {
  62.       queuestmt();
  63.      }
  64. ;
  65.  
  66. cond
  67.      : /* */
  68.      | cond OR cond {
  69.           pushoper($2);
  70.      }
  71.      | cond AND cond {
  72.       pushoper($2);
  73.      }
  74.      | term
  75. ;
  76.  
  77. actionset
  78.      : action ';' actionset
  79.      | action ';'
  80.      | action
  81. ;
  82.  
  83. action
  84.      : PRINT {
  85.       queueact($1);
  86.      }
  87.      | NEXT {
  88.           queueact($1);
  89.      }
  90.      | error
  91. ;
  92.  
  93. term
  94.      : SRCPORT oper tcpport {
  95.       pushkey(SRCPORT);
  96.       pushval($3);
  97.       pushoper($2);
  98.      }
  99.      | DSTPORT oper tcpport {
  100.       pushkey(DSTPORT);
  101.       pushval($3);
  102.       pushoper($2);
  103.      }
  104.      | HOST oper hostspec {
  105.           pushkey(SRCADDR);
  106.           pushval($3);
  107.           pushoper($2);
  108.           pushkey(DSTADDR);
  109.           pushval($3);
  110.           pushoper($2);
  111.           pushoper(OR);
  112.      }
  113.      | SRCADDR oper hostspec {
  114.       pushkey(SRCADDR);
  115.       pushval($3);
  116.       pushoper($2);
  117.      }
  118.      | DSTADDR oper hostspec {
  119.       pushkey(DSTADDR);
  120.       pushval($3);
  121.       pushoper($2);
  122.      }
  123.      | NET netop hostspec {
  124.           unsigned long mask = getmask($3);
  125.           pushval($3);
  126.           pushval(mask);
  127.           pushoper(MASKOPER); /* net & mask */
  128.           pushkey(SRCNET);
  129.           pushval(mask);
  130.           pushoper(MASKOPER); /* SRCADDR & mask */
  131.           pushoper($2);
  132.           pushval($3);
  133.           pushval(mask);
  134.           pushoper(MASKOPER); /* net & mask */
  135.           pushkey(DSTNET);
  136.           pushval(mask);      /* dstaddr & mask */
  137.           pushoper(MASKOPER);
  138.           pushoper($2);
  139.           pushoper(OR);
  140.      }
  141.      | NET netop hostspec '/' netmask {
  142.           pushval($3);
  143.           pushval($5);
  144.           pushoper(MASKOPER); /* net & mask */
  145.           pushkey(SRCNET);
  146.           pushval($5);
  147.           pushoper(MASKOPER); /* srcaddr & mask */
  148.           pushoper($2);
  149.           pushval($3);
  150.           pushval($5);
  151.           pushoper(MASKOPER); /* net & mask */
  152.           pushkey(DSTNET);
  153.           pushval($5);
  154.           pushoper(MASKOPER); /* dstaddr & mask */
  155.           pushoper($2);
  156.           pushoper(OR);
  157.      }
  158.      | SRCNET netop hostspec {
  159.       unsigned long mask = getmask($3);
  160.       pushval($3);
  161.       pushval(mask);
  162.       pushoper(MASKOPER);
  163.       pushkey(SRCNET);
  164.       pushval(mask);
  165.       pushoper(MASKOPER);
  166.       pushoper($2);
  167.      }
  168.      | DSTNET netop hostspec {
  169.       unsigned long mask = getmask($3);
  170.       pushval($3);
  171.           pushval(mask);
  172.       pushoper(MASKOPER);
  173.       pushkey(DSTNET);
  174.       pushval(mask);
  175.       pushoper(MASKOPER);
  176.       pushoper($2);
  177.      }
  178.      | SRCNET netop hostspec '/' netmask {
  179.       pushval($3);
  180.       pushval($5);
  181.       pushoper(MASKOPER);
  182.       pushkey(SRCNET);
  183.       pushval($5);
  184.       pushoper(MASKOPER);
  185.       pushoper($2);
  186.      }
  187.      | DSTNET netop hostspec '/' netmask {
  188.       pushval($3);
  189.           pushval($5);
  190.       pushoper(MASKOPER);
  191.       pushkey(DSTNET);
  192.       pushval($5);
  193.       pushoper(MASKOPER);
  194.       pushoper($2);
  195.      }
  196.      | DATE oper datespec {
  197.           switch($2){
  198.       case '>':
  199.       case LEQ:
  200.            pushkey($1);
  201.            pushval($3 + (24*3600-1));
  202.            pushoper($2);
  203.            break;
  204.       case '=':
  205.            pushkey($1);
  206.            pushval($3);
  207.            pushoper(GEQ);
  208.            pushkey($1);
  209.            pushval($3 + (24*3600-1));
  210.            pushoper(LEQ);
  211.            pushoper(AND);
  212.            break;
  213.       case NEQ:
  214.            pushkey($1);
  215.            pushval($3);
  216.            pushoper('<');
  217.            pushkey($1);
  218.            pushval($3 + (24*3600-1));
  219.            pushoper('>');
  220.            pushoper(OR);
  221.            break;
  222.       default:
  223.            pushkey($1);
  224.            pushval($3);
  225.            pushoper($2);
  226.            break;
  227.       }
  228.      }
  229.      | TIME oper timespec {
  230.       pushkey($1);
  231.       pushval($3);
  232.       pushoper($2);
  233.      }
  234.      | SINCE datespec timespec {
  235.           pushkey(DATE);
  236.           pushval($2+$3);
  237.           pushoper(GEQ);
  238.      }
  239.      | SINCE datespec {
  240.           pushkey(DATE);
  241.           pushval($2);
  242.           pushoper(GEQ);
  243.      }
  244.      | BEFORE datespec timespec {
  245.           pushkey(DATE);
  246.           pushval($2+$3);
  247.           pushoper('<');
  248.      }
  249.      | BEFORE datespec {
  250.           pushkey(DATE);
  251.           pushval($2);
  252.           pushoper('<');
  253.      }
  254.      | '(' cond ')'
  255.      | '!' term { pushuoper($1); }
  256.      | NOT term { pushuoper('!'); }
  257.      | error { pushval(0); }
  258. ;
  259.  
  260. oper
  261.      : '=' {$$ = $1;}
  262.      | '>' {$$ = $1;}
  263.      | '<' {$$ = $1;}
  264.      | NEQ {$$ = $1;}
  265.      | LEQ {$$ = $1;}
  266.      | GEQ {$$ = $1;}
  267.      | error { pushoper(0); }
  268. ;
  269.  
  270. netop
  271.      : '=' {$$ = $1;}
  272.      | NEQ {$$ = $1;}
  273.      | error { pushoper(0); }
  274. ;
  275.  
  276. datespec
  277.      : BYTEVAL '/' BYTEVAL {
  278.       int year = getyear();
  279.       if(year < 100)
  280.            year += 1900;
  281.       if($1 < 1 || $1 > 12 ||
  282.          !validmday($1, $3, year)){
  283.            fprintf(stderr, "\"%s\", line %d: malformed date.\n",
  284.                getfilename(), getlinenum());
  285.            parse_error = 1;
  286.       }
  287.       else
  288.            $$ = makedate($1, $3, year);
  289.      }
  290.      | BYTEVAL '/' BYTEVAL '/' BYTEVAL {
  291.       int year = $5;
  292.       if(year < 100)
  293.            year += 1900;
  294.       if($1 < 1 || $1 > 12 ||
  295.          !validmday($1, $3, year)){
  296.            fprintf(stderr, "\"%s\", line %d: malformed date.\n",
  297.                getfilename(), getlinenum());
  298.            parse_error = 1;
  299.       }
  300.       else
  301.            $$ = makedate($1, $3, year);
  302.      }
  303.      | TODAY {
  304.           $$ = today();
  305.      }
  306.      | YEST {
  307.           $$ = today() - (24*3600);
  308.      }
  309.      | error { $$ = 0; }
  310. ;           
  311.  
  312. timespec
  313.      : BYTEVAL ':' BYTEVAL {
  314.       $$ = $1*3600 + $3*60;
  315.      }
  316.      | BYTEVAL ':' BYTEVAL ':' BYTEVAL {
  317.       $$ = $1*3600 + $3*60 + $5;
  318.      }
  319.      | error { $$ = 0; }
  320. ;
  321.  
  322.  
  323.  
  324. tcpport
  325.      : integer { $$ = $1; }
  326.      | STRING {
  327.       struct servent *se;
  328.       if((se = getservbyname($1,
  329.                  udptcpflag == TCPDATA ? "tcp" : "udp"))){
  330.            $$ = htonl(se->s_port);
  331.       }
  332.       else {
  333.            fprintf(stderr, "\"%s\", line %d: unknown service: %s\n", 
  334.                getfilename(), getlinenum(),$1);
  335.            parse_error = 1;
  336.            $$ = 0;
  337.       }
  338.       free($1);
  339.      }
  340.      | error {$$ = 0;}
  341. ;
  342.  
  343. integer
  344.      : INTEGER {
  345.           $$ = $1;
  346.      }
  347.      | BYTEVAL {
  348.           $$ = $1;
  349.      }
  350. ;
  351.  
  352. hostspec
  353.      : ip_address { $$ = $1;}
  354.      | STRING {
  355.           struct hostent *he;
  356.       struct {
  357.            int ipaddr:32;
  358.       } host;
  359.       if((he = gethostbyname($1))){
  360.            memcpy(&host, he->h_addr_list[0], 4);
  361.            $$ = host.ipaddr;
  362.       }
  363.       else {
  364.            fprintf(stderr, "\"%s\", line %d: unknown host: %s\n", 
  365.                getfilename(), getlinenum(), $1);
  366.            $$ = 0;
  367.            parse_error = 1;
  368.       }
  369.       free($1);
  370.      }
  371.      | error { $$ = 0; }
  372. ;
  373.  
  374. netmask
  375.      : ip_address { $$=$1;}
  376.      
  377. ;
  378.  
  379. ip_address
  380.     : BYTEVAL '.' BYTEVAL '.' BYTEVAL '.' BYTEVAL {
  381.          $$ = (($1 << 24 | $3 << 16 | $5 << 8 | $7));
  382.     }
  383. ;
  384.  
  385. %%
  386.  
  387.      struct keywords keywords[] = {
  388.      { "dstport", DSTPORT },
  389.      { "srcport", SRCPORT },
  390.      { "srchost", SRCADDR },
  391.      { "dsthost", DSTADDR },
  392.      { "srcnet", SRCNET },
  393.      { "dstnet", DSTNET },
  394.      { "host", HOST },
  395.      { "net", NET },
  396.      { "time", TIME },
  397.      { "date", DATE },
  398.      { "since", SINCE },
  399.      { "before", BEFORE },
  400.      { "today", TODAY },
  401.      { "yesterday", YEST },
  402.      { "or", OR },
  403.      { "and", AND },
  404.      { "not", NOT }, 
  405.      { "print", PRINT },
  406.      { "next", NEXT },
  407.      { (char *)0, 0 }
  408.      };
  409.  
  410. void
  411. yyerror()
  412. {
  413.      fprintf(stderr, "\"%s\", line %d: syntax error.\n",
  414.          getfilename(), getlinenum());
  415.      parse_error = 1;
  416. }
  417.  
  418. unsigned long
  419. getmask(unsigned long addr)
  420. {
  421.      if(IN_CLASSA(addr))
  422.       return IN_CLASSA_NET;
  423.      else if(IN_CLASSB(addr))
  424.       return IN_CLASSB_NET;
  425.      else if(IN_CLASSC(addr))
  426.       return IN_CLASSC_NET;
  427.      else
  428.       return 0xffffffff;
  429. }
  430.  
  431.